package com.tj.test.sys.service.impl;

import com.tj.test.sys.dao.PaperMapper;
import com.tj.test.sys.po.Paper;
import com.tj.test.sys.service.IPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PaperService implements IPaperService {
    @Autowired
    private PaperMapper mapper;

    @Override
    //查询所有试卷表信息
    public List<Paper> selectAllPaper(Paper paper) {
        List<Paper> list=new ArrayList<>();
        if (!mapper.selectAllPaper(paper).isEmpty()){
            list=mapper.selectAllPaper(paper);
        }
        return list;
    }

    @Override
    //插入试卷信息
    public int insertPaper(Paper paper) {
        int temp=0;
        /* 业务判断
         * （1）名称不能重复
         * （2）增加之前先查询*/
        try{
            temp=mapper.selectPaperById(paper);
            if (temp<1){
                temp=mapper.insertPaper(paper);
            }else{
                return temp;
            }
        }catch (Exception e){
            return -2;
        }
        return temp;
    }

    @Override
    //删除试卷表信息
    public int deletePaper( Paper paper) {
        int temp=0;
        try{
            temp=mapper.selectPaperById(paper);
            if (temp>0){
                temp=mapper.deletePaper(paper);
            }else{
                return -2;
            }
        }catch (Exception e){
            return -1;
        }
        return temp;
    }

    @Override
    //修改试卷表信息
    public int updatePaper(Paper paper) {
        int temp=0;
        try{
            temp=mapper.selectPaperById(paper);
            if (temp>0){
                temp=mapper.updatePaper(paper);
            }else{
                return temp;
            }
        }catch (Exception e){
            System.out.println(e.getStackTrace());
            return -1;
        }
        return temp;
    }
}
